package com.data.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.modules.system.entity.Account;
import com.data.modules.system.entity.User;
import com.data.modules.system.service.IAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 测试
 * @Author: data-boot
 * @Date:   2022-11-08
 * @Version: V1.0
 */
@Api(tags="测试")
@RestController
@RequestMapping("/sys/account")
@Slf4j
public class AccountController extends BootController<Account, IAccountService> {
	@Autowired
	private IAccountService accountService;


	/**
	 * 保存用户信息接口
	 *
	 * @param
	 * @return
	 */
	@ApiOperation("保存用户信息")
	@PostMapping(value = "/saveUser")
	public Result<String> saveUser(HttpServletRequest request, @RequestBody User user) {
		Result<String> result = new Result<String>();
		Account account = new Account();
		//account.setCreateBy("1");
		account.setAccountMobile(user.getMobile());
		account.setAccountEmail(user.getMobile());
		account.setAccountPassword(user.getMobile());

		accountService.save(account);

		return result;
	}



	@ApiOperation("测试用户信息")
	//@PostMapping(value = "/getProjectList")
	@GetMapping(value = "/getProjectList")
	//@NeedToken
	public Result<String> getProjectList(HttpServletRequest request) {
		String auth = request.getHeader("Authorization");

		/*String userId = JwtAuthUtil.checkHead(auth);

		System.out.println("--userId--:"+userId);*/
		Result<String> result = new Result<String>();
		Account account = new Account();
		//account.setCreateBy("1");


		return result;
	}


	/**
	 * 分页列表查询
	 *
	 * @param account
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "测试-分页列表查询")
	@ApiOperation(value="测试-分页列表查询", notes="测试-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Account account,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Account> queryWrapper = QueryGenerator.initQueryWrapper(account, req.getParameterMap());
		Page<Account> page = new Page<Account>(pageNo, pageSize);
		IPage<Account> pageList = accountService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param account
	 * @return
	 */
	@AutoLog(value = "测试-添加")
	@ApiOperation(value="测试-添加", notes="测试-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Account account) {
		accountService.save(account);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param account
	 * @return
	 */
	@AutoLog(value = "测试-编辑")
	@ApiOperation(value="测试-编辑", notes="测试-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Account account) {
		accountService.updateById(account);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "测试-通过id删除")
	@ApiOperation(value="测试-通过id删除", notes="测试-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		accountService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "测试-批量删除")
	@ApiOperation(value="测试-批量删除", notes="测试-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.accountService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "测试-通过id查询")
	@ApiOperation(value="测试-通过id查询", notes="测试-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Account account = accountService.getById(id);
		if(account==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(account);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param account
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Account account) {
        return super.exportXls(request, account, Account.class, "测试");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Account.class);
    }

}
